package com.red.battleworm.view {
	import flash.text.TextField;	
	
	import fl.containers.ScrollPane;	
	
	import com.red.net.SocketEvent;	
	import com.red.battleworm.protocols.ProtocolFactory;	
	import com.red.battleworm.protocols.Protocol;	
	import com.red.battleworm.model.NetProxy;	
	
	import flash.events.MouseEvent;	
	
	import fl.controls.Button;	
	
	import flash.display.SimpleButton;	
	import flash.display.Sprite;	
	
	import org.puremvc.as3.interfaces.INotification;	
	
	import com.red.battleworm.AppMain;	
	
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	/**
	 * @author Administrator
	 */
	public class RoomMediator extends Mediator {
		public function RoomMediator(viewComponent : Object = null) {
			super("RoomMediator", viewComponent);
		}
		
		override public function listNotificationInterests():Array 
        {
            return [ AppMain.SHOWROOM];
		}
        
        override public function handleNotification( note:INotification ):void 
        {
            switch ( note.getName() ) {
            	case AppMain.SHOWROOM:
            		init(XML(note.getBody()));
            		break;
            }
        }
        
        protected function get stage():Sprite{
            return viewComponent as Sprite;
        }
        
        private var _page:Sprite;
        private var _randroom:Button;
        private var _conn:NetProxy;
        private var _dispList:Array;
        private var _roomList:MovieClip;
		
        public function init(xml : XML) : void {
        	_page = new RoomPage();
        	_randroom = _page.randroom;
        	_roomList = _page.roomList;
			_randroom.addEventListener(MouseEvent.MOUSE_UP, onRandroom);
			_conn = NetProxy(facade.retrieveProxy("NetProxy"));
			//初始化房间列表
			addBox(xml);
			stage.addChild(_page);
		}
		
		private function addBox(xml : XML) : void{
			var board:Sprite = null;
			var panel:Sprite = new Sprite();
			for each (var room:XML in xml.room){
				board = new Sprite();
				board.height = 50;
				var id:TextField = new TextField();
				id.text = room.@id;
				board.addChild(id);
				_dispList.push(board);
			}
			for(var i:int=0; i<_dispList.length; i++){
				_dispList[i].y = i*_dispList[i].height;
				panel.addChild(_dispList[i]);
			}
			var scrollPane:ScrollPane = new ScrollPane();
			scrollPane.source = panel;
			_roomList.addChild(scrollPane);
		}
		
		public function destroy() : void {
			_randroom.removeEventListener(MouseEvent.MOUSE_UP, onRandroom);
			stage.removeChild(_page);
		}
		
		public function buildRoomList(xml:String){
			var list:XML = XML(xml);
			
		}
		
		private function onRandroom(e:MouseEvent) : void {
			//发送房间选择信息
			var protocol:Protocol = ProtocolFactory.getChooseRoomProtocol(1);
		}
		
		private function onReceive(e:SocketEvent) : void {
			buildRoomList(e.data);
		}
	}
}
